package com.lmiot.xyclick.Util;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lmiot.xyclick.ActionDetailBean.DetailBean;
import com.lmiot.xyclick.App.MyApp;
import com.lmiot.xyclick.Auto.AutoUtils;
import com.lmiot.xyclick.Bean.InitFloatBean;
import com.lmiot.xyclick.Bean.RecordBean;
import com.lmiot.xyclick.Bean.SQL.ActionBean;
import com.lmiot.xyclick.Bean.SQL.ActionBeanSqlUtil;
import com.lmiot.xyclick.Bean.SQL.AutoBean;
import com.lmiot.xyclick.Bean.SQL.AutoBeanSqlUtil;
import com.lkx.liandianqinew.R;
import com.xiaoyi.intentsdklibrary.Bean.PointBean;
import com.xiaoyi.intentsdklibrary.SDK.Action.ActionAsSDK;
import com.xiaoyi.intentsdklibrary.SDK.Action.ActionNormalSDK;
import com.xiaoyi.intentsdklibrary.SDK.Interface.OnAsKeyListener;
import com.xiaoyi.intentsdklibrary.SDK.SDK;
import com.yhao.floatwindow.FloatUtil;
import com.yhao.floatwindow.FloatWindow;
import com.yhao.floatwindow.MoveType;
import com.youyi.yypermissionlibrary.SDK.YYPerUtils;
import com.youyi.yyviewsdklibrary.Dialog.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class RecordSDK {
    private static final String TAG = "RecordSDK";
    public static boolean mIsRecord = false;
    public static OnRecordListener mOnRecordListener;
    private static final RecordSDK ourInstance = new RecordSDK();
    private List<ActionBean> mActionBeanList;
    private long mActionTime0;
    private AutoBean mAutoBean;
    private Dialog mDialog;
    private int mDuration;
    private int mEndX;
    private int mEndY;
    private Handler mHandler = new Handler() {
        /* class com.lmiot.xyclick.Util.RecordSDK.AnonymousClass1 */

        public void handleMessage(Message message) {
            super.handleMessage(message);
            if (message.what == 528) {
                RecordSDK.this.showRecordDialog();
            }
        }
    };
    private Intent mIntent;
    private List<PointBean> mPointBeanList = new ArrayList();
    private View mRecordView;
    private TextView mRecoreText;
    private long mStartTime;
    private int mStartX;
    private int mStartY;

    public interface OnRecordListener {
        void result(RecordBean recordBean);
    }

    public enum RecordType {
        RECORD_CLICK,
        RECORD_LONGCLICK,
        RECORD_SWIPE
    }

    public static RecordSDK getInstance() {
        return ourInstance;
    }

    private RecordSDK() {
    }

    public void initRecordView() {
        try {
            FloatWindow.destroy("floatViewRecord");
            View inflate = View.inflate(MyApp.getContext(), R.layout.layout_float_record, null);
            FloatWindow.with(MyApp.getContext()).setView(inflate).setTag("floatViewRecord").setMoveType(MoveType.slide).setDesktopShow(true).setMoveStyle(500, new AccelerateInterpolator()).build();
            final ImageView imageView = (ImageView) inflate.findViewById(R.id.id_recording);
            final ImageView imageView2 = (ImageView) inflate.findViewById(R.id.id_unrecord);
            ((ImageView) inflate.findViewById(R.id.id_close)).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    RecordSDK.this.stopRecord();
                    FloatManager.hide(InitFloatBean.FloatType.record);
                }
            });
            this.mRecoreText = (TextView) inflate.findViewById(R.id.id_record_text);
            inflate.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    ClickUtils.onlyVibrate(MyApp.getContext());
                    if (!RecordSDK.mIsRecord) {
                        imageView.setVisibility(0);
                        imageView2.setVisibility(8);
                        RecordSDK.this.mRecoreText.setText("录制中……");
                        RecordSDK.mIsRecord = true;
                        RecordSDK.this.startRecord();
                        return;
                    }
                    RecordSDK.this.stopRecord();
                    int size = RecordSDK.this.mActionBeanList.size();
                    if (size > 1) {
                        RecordSDK.this.mActionBeanList.remove(size - 1);
                    }
                    ActionBeanSqlUtil.getInstance().addList(RecordSDK.this.mAutoBean.getAutoID(), RecordSDK.this.mActionBeanList);
                    AutoBeanSqlUtil.getInstance().add(RecordSDK.this.mAutoBean);
                    imageView.setVisibility(8);
                    imageView2.setVisibility(0);
                    RecordSDK.this.mRecoreText.setText("点击开始录制");
                    FloatManager.hide(InitFloatBean.FloatType.record);
                    ToastUtil.warning("录制成功！");
                    AutoUtils.editAuto(MyApp.getContext(), RecordSDK.this.mAutoBean);
                }
            });
//            inflate.setVisibility(View.GONE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void listenUserAction(final Context context, OnRecordListener onRecordListener) {
        mOnRecordListener = onRecordListener;
        Dialog dialog = this.mDialog;
        if (dialog != null) {
            if (dialog.isShowing()) {
                this.mDialog.dismiss();
            }
            this.mDialog = null;
        }
        mIsRecord = true;
        this.mPointBeanList.clear();
        this.mDialog = new Dialog(context);
        if (!YYPerUtils.hasOp()) {
            ActionNormalSDK.getInstance().gotoFloatPermissionSetting(context);
            return;
        }
        this.mDialog.setCancelable(false);
        this.mDialog.requestWindowFeature(1);
        this.mDialog.setContentView(R.layout.record_layout);
        Window window = this.mDialog.getWindow();
        window.setBackgroundDrawableResource(R.color.transparent);
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.dimAmount = 0.0f;
        window.setGravity(17);
        window.setFlags(131072, 131072);
        if (Build.VERSION.SDK_INT >= 26) {
            window.setType(2038);
        } else {
            window.setType(2003);
        }
        attributes.width = -1;
        attributes.height = -1;
        window.setAttributes(attributes);
        this.mDialog.show();
        View findViewById = this.mDialog.findViewById(R.id.id_record_view);
        this.mRecordView = findViewById;
        findViewById.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int action = motionEvent.getAction();
                if (action == 0) {
                    RecordSDK.this.mStartX = (int) motionEvent.getX();
                    RecordSDK.this.mStartY = (int) motionEvent.getY();
                    if (ActionNormalSDK.getInstance().isScreenPortrait(context)) {
                        RecordSDK.this.mStartY += StateBarUtil.getStatusBarHeight(context);
                    } else {
                        RecordSDK.this.mStartX += StateBarUtil.getStatusBarHeight(context);
                    }
                    RecordSDK.this.mStartTime = System.currentTimeMillis();
                    RecordSDK.this.mPointBeanList.clear();
                    RecordSDK.this.mPointBeanList.add(0, new PointBean(RecordSDK.this.mStartX, RecordSDK.this.mStartY));
                } else if (action == 1) {
                    RecordSDK.this.mEndX = (int) motionEvent.getX();
                    RecordSDK.this.mEndY = (int) motionEvent.getY();
                    if (ActionNormalSDK.getInstance().isScreenPortrait(context)) {
                        RecordSDK.this.mEndY += StateBarUtil.getStatusBarHeight(context);
                    } else {
                        RecordSDK.this.mEndX += StateBarUtil.getStatusBarHeight(context);
                    }
                    RecordSDK.this.mDuration = (int) (System.currentTimeMillis() - RecordSDK.this.mStartTime);
                    RecordSDK.this.mPointBeanList.add(new PointBean(RecordSDK.this.mEndX, RecordSDK.this.mEndY));
                    RecordSDK.this.hideRecord();
                } else if (action == 2) {
                    int x = (int) motionEvent.getX();
                    int y = (int) motionEvent.getY();
                    if (ActionNormalSDK.getInstance().isScreenPortrait(context)) {
                        y += StateBarUtil.getStatusBarHeight(context);
                    } else {
                        x += StateBarUtil.getStatusBarHeight(context);
                    }
                    RecordSDK.this.mPointBeanList.add(new PointBean(x, y));
                }
                return true;
            }
        });
    }

    public void hideRecord() {
        RecordBean recordBean = new RecordBean();
        int abs = Math.abs(this.mEndX - this.mStartX);
        int abs2 = Math.abs(this.mEndY - this.mStartY);
        if (abs >= 50 || abs2 >= 50) {
            recordBean.setRecordType(RecordType.RECORD_SWIPE);
            recordBean.setPointBeanList(this.mPointBeanList);
            recordBean.setDuration(this.mDuration);
            mOnRecordListener.result(recordBean);
        } else if (this.mDuration <= 500) {
            recordBean.setRecordType(RecordType.RECORD_CLICK);
            recordBean.setPointBeanList(this.mPointBeanList);
            recordBean.setDuration(this.mDuration);
            mOnRecordListener.result(recordBean);
        } else {
            recordBean.setRecordType(RecordType.RECORD_LONGCLICK);
            recordBean.setPointBeanList(this.mPointBeanList);
            recordBean.setDuration(this.mDuration);
            mOnRecordListener.result(recordBean);
        }
        try {
            Dialog dialog = this.mDialog;
            if (dialog != null && dialog.isShowing()) {
                this.mDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isRecording() {
        try {
            return this.mDialog.isShowing();
        } catch (Exception unused) {
            return false;
        }
    }

    private void startRecord() {
        AutoBean autoBean = new AutoBean();
        this.mAutoBean = autoBean;
        autoBean.setAutoID(TimeUtils.createAutoID());
        this.mAutoBean.setAutoType("click");
        AutoBean autoBean2 = this.mAutoBean;
        autoBean2.setAutoName("录制动作" + TimeUtils.getAlarmTimeDay());
        this.mAutoBean.setRepeatNum(1);
        this.mAutoBean.setIsEnable(true);
        this.mAutoBean.setCreateTime(TimeUtils.createDay());
        this.mAutoBean.setGroupID("666");
        this.mAutoBean.setSortNum(AutoBeanSqlUtil.getInstance().searchAll().size() + 1);
        this.mActionBeanList = new ArrayList();
        SDK.mOnAsKeyListener = new OnAsKeyListener() {
            @Override
            public void result(KeyType keyType) {
                ActionBean actionBean = new ActionBean();
                actionBean.setActionID(TimeUtils.createActionID());
                actionBean.setCreateTime(TimeUtils.getCurrentTime());
                actionBean.setAutoID(RecordSDK.this.mAutoBean.getAutoID());
                actionBean.setEnable(true);
                actionBean.setSortNum(RecordSDK.this.mActionBeanList.size() + 1);
                int i = keyType.ordinal();
                if (i == 1) {
                    actionBean.setActionName("模拟返回");
                    actionBean.setActionType(Constants.ACTION_TYPE_RES_BACK);
                } else if (i == 2) {
                    actionBean.setActionName("返回桌面");
                    actionBean.setActionType(Constants.ACTION_TYPE_RES_HOME);
                } else if (i == 3) {
                    actionBean.setActionName("打开最近任务");
                    actionBean.setActionType(Constants.ACTION_TYPE_RES_RECENT);
                }
                RecordSDK.this.addAction(actionBean);
            }
        };
        listenUserAction(MyApp.getContext(), new OnRecordListener() {
            @Override
            public void result(final RecordBean recordBean) {
                ActionBean actionBean = new ActionBean();
                actionBean.setActionID(TimeUtils.createActionID());
                actionBean.setCreateTime(TimeUtils.getCurrentTime());
                actionBean.setAutoID(RecordSDK.this.mAutoBean.getAutoID());
                actionBean.setEnable(true);
                DetailBean detailBean = new DetailBean();
                detailBean.setDruation(recordBean.getDuration());
                actionBean.setSortNum(RecordSDK.this.mActionBeanList.size() + 1);
                final ArrayList arrayList = new ArrayList();
                for (PointBean pointBean : recordBean.getPointBeanList()) {
                    arrayList.add(new PointBean(pointBean.getX() + DataUtil.getRecordOffSetX(MyApp.getContext()), pointBean.getY() + DataUtil.getRecordOffSetY(MyApp.getContext())));
                }
                ActionAsSDK.getInstance().init(MyApp.getContext());
                SDK.isRunning = true;
                final PointBean pointBean2 = (PointBean) arrayList.get(0);
                RecordType recordType = recordBean.getRecordType();
                if (recordType == RecordType.RECORD_CLICK) {
                    actionBean.setActionName("单击");
                    actionBean.setActionType(Constants.ACTION_TYPE_RES_CLICK);
                    detailBean.setPointBean(new PointBean(pointBean2.getX(), pointBean2.getY()));
                    detailBean.setRepeat(1);
                    Thread mThread = new Thread() {
                        public void run() {
                            int i = 0;
                            while (true) {
                                if (i >= 1000) {
                                    break;
                                }
                                try {
                                    if (!RecordSDK.this.mDialog.isShowing()) {
                                        break;
                                    }
                                    sleep(5);
                                    i++;
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                    return;
                                }
                            }
                            ActionAsSDK.getInstance().clickXYDureation(pointBean2.getX(), pointBean2.getY(), recordBean.getDuration());
                            RecordSDK.this.mHandler.sendEmptyMessage(528);
                        }
                    };
                    mThread.start();
                } else if (recordType == RecordType.RECORD_LONGCLICK) {
                    actionBean.setActionName("长按");
                    actionBean.setActionType(Constants.ACTION_TYPE_RES_LONG_CLICK_TIMES_MS);
                    detailBean.setPointBean(new PointBean(pointBean2.getX(), pointBean2.getY()));
                    detailBean.setDruation(recordBean.getDuration());
                    detailBean.setRepeat(1);
                    Thread mThread = new Thread() {
                        /* class com.lmiot.xyclick.Util.RecordSDK.AnonymousClass6.AnonymousClass2 */

                        public void run() {
                            int i = 0;
                            while (true) {
                                if (i >= 1000) {
                                    break;
                                }
                                try {
                                    if (!RecordSDK.this.mDialog.isShowing()) {
                                        break;
                                    }
                                    sleep(5);
                                    i++;
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                    return;
                                }
                            }
                            ActionAsSDK.getInstance().clickXYDureation(pointBean2.getX(), pointBean2.getY(), recordBean.getDuration());
                            RecordSDK.this.mHandler.sendEmptyMessage(528);
                        }
                    };
                    mThread.start();
                } else if (recordType == RecordType.RECORD_SWIPE) {
                    actionBean.setActionName("根据路径滑动");
                    actionBean.setActionType(Constants.ACTION_TYPE_RES_SWIP_PATHLISt);
                    detailBean.setPointBeanList(arrayList);
                    detailBean.setDruation(recordBean.getDuration());
                    detailBean.setRepeat(1);
                    Thread mThread = new Thread() {
                        public void run() {
                            int i = 0;
                            while (true) {
                                if (i >= 1000) {
                                    break;
                                }
                                try {
                                    if (!RecordSDK.this.mDialog.isShowing()) {
                                        break;
                                    }
                                    sleep(5);
                                    i++;
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                    return;
                                }
                            }
                            ActionAsSDK.getInstance().pathList(recordBean.getDuration(), arrayList);
                            RecordSDK.this.mHandler.sendEmptyMessage(528);
                        }
                    };
                    mThread.start();
                }
                actionBean.setDetail(new Gson().toJson(detailBean));
                RecordSDK.this.addAction(actionBean);
            }
        });
    }

    public void stopRecord() {
        try {
            Dialog dialog = this.mDialog;
            if (dialog != null) {
                if (dialog.isShowing()) {
                    this.mDialog.dismiss();
                }
                this.mDialog = null;
            }
            mIsRecord = false;
            mOnRecordListener = null;
            SDK.mOnAsKeyListener = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void addAction(ActionBean actionBean) {
        if (this.mActionBeanList.size() == 0) {
            this.mActionTime0 = System.currentTimeMillis();
        } else {
            long currentTimeMillis = System.currentTimeMillis() - this.mActionTime0;
            int i = 1;
            ActionBean actionBean2 = this.mActionBeanList.get(this.mActionBeanList.size() - 1);
            if (FloatUtil.getAutoMs(MyApp.getContext())) {
                actionBean2.setDelay((int) currentTimeMillis);
            } else {
                int i2 = (int) (currentTimeMillis / 1000);
                if (i2 != 0) {
                    i = i2;
                }
                actionBean2.setDelay(i);
            }
            this.mActionTime0 = System.currentTimeMillis();
        }
        this.mActionBeanList.add(actionBean);
        TextView textView = this.mRecoreText;
        if (textView != null) {
            textView.setText("录制中…(" + this.mActionBeanList.size() + ")");
        }
    }

    public void showRecordDialog() {
        try {
            if (this.mDialog != null) {
                this.mHandler.post(new Runnable() {
                    public void run() {
                        if (RecordSDK.this.mDialog != null) {
                            RecordSDK.this.mDialog.show();
                        }
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
